# -*- coding: utf-8 -*-
# @Time    : 2019/3/2 9:17
# @Author  : shaoeric
# @Email   : shaoeric@foxmail.com

import numpy as np
import matplotlib.pyplot as plt
import cv2
import os

"""
本文件主要完成图片皮肤区域的色彩特征的提取以及统计功能
"""
def get_skin_hsv(file, train=True):
    if train:
        path = 'train'
    else:
        path = 'test'
    img = cv2.imread('{}/origin/{}'.format(path, file))
    img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

    ground = cv2.imread('{}/ground/{}'.format(path, file.replace('.jpg', '.png').replace('.jpeg', '.png')), cv2.IMREAD_GRAYSCALE)

    h,s,v = cv2.split(img_hsv)

    skin_h = h[ground > 0]  # 皮肤位置的h值
    skin_s = s[ground > 0]  # 皮肤位置的s值
    skin_v = v[ground > 0]  # 贫富位置的v值
    #
    return skin_h, skin_s, skin_v


def plot_histogram(skin_h, skin_s, skin_v):
    plt.subplot(2,2,1)
    plt.hist(skin_h, bins=50, normed=True)
    plt.title('hue histogram')

    plt.subplot(2,2,2)
    plt.hist(skin_s, bins=50, normed=True)
    plt.title('saturation histogram')

    plt.subplot(2,2,3)
    plt.hist(skin_v, bins=50, normed=True)
    plt.title('value/brightness histogram')
    plt.tight_layout()
    plt.show()

def extract_skin_HSV():
    H, S, V = [], [], []
    i = 0
    for file in os.listdir('train/origin'):
        h,s,v = get_skin_hsv(file)
        H.append(h)
        S.append(s)
        V.append(v)
        i += 1
        print('finish ', i)

    np.save('train/hue.npy', np.array(H))
    np.save('train/saturation.npy', np.array(S))
    np.save('train/value.npy', np.array(V))

def main():
    H = np.load('train/hue.npy')
    S = np.load('train/saturation.npy')
    V = np.load('train/value.npy')
    plot_histogram(H, S, V)

# extract_skin_HSV()
main()